package neusoft.example.com.yuejian06.Model;


import java.util.List;

import neusoft.example.com.yuejian06.Bean.Competition;
import neusoft.example.com.yuejian06.PublicActivity;
import neusoft.example.com.yuejian06.RetrofitListener;
import neusoft.example.com.yuejian06.Service.CompetitionService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CompetitionListModel {
    private Retrofit retrofit;
    private CompetitionService service;
    private PublicActivity publicActivity;

    public CompetitionListModel(){
        publicActivity=new PublicActivity();
        retrofit=new Retrofit.Builder()
                .baseUrl(publicActivity.getIp()+"api/index/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(CompetitionService.class);
    }

    public void getCompetitionList(final RetrofitListener listener){
        Call<List<Competition>> call=service.getCompetitionList();
        call.enqueue(new Callback<List<Competition>>() {
            @Override
            public void onResponse(Call<List<Competition>> call, Response<List<Competition>> response) {
                if(response.isSuccessful() && response!=null){
                    listener.onResponse(response.body());
                }else{
                    listener.onFail("评论的！");//
                }
            }

            @Override
            public void onFailure(Call<List<Competition>> call, Throwable t) {
                listener .onFail(t.toString());
            }
        });
    }

}
